package de.upb.messerschmidt.set;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import de.upb.messerschmidt.set.entity.Attendee;
import de.upb.messerschmidt.set.entity.Person;
import de.upb.messerschmidt.set.entity.Talk;
import de.upb.messerschmidt.set.jdo.PMF;
import de.upb.messerschmidt.set.util.Utils;

/**
 * Saves the {@link Attendee} information for specific {@link Talk}s.
 * 
 * @author Tim Messerschmidt
 * 
 */
public class RegisterAttendTalkServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String personString = req.getParameter("person");
		String talkString = req.getParameter("talk");
		String talkParam = talkString.split(" at")[0];

		if (personString == null || talkString == null) {
			resp.setStatus(400);
			resp.getWriter().println(
					"Registration failed - please fill in all fields");
		} else {
			PersistenceManager pm = PMF.get().getPersistenceManager();

			Query personQuery = pm.newQuery(Person.class);
			personQuery
					.setFilter("name == nameParam && prename == prenameParam");
			personQuery
					.declareParameters("String nameParam, String prenameParam");
			personQuery.setOrdering("name desc");

			// Get the user
			Person user = null;
			String[] name = Utils.nameParser(personString);

			List<Person> persons = (List<Person>) personQuery.execute(name[0],
					name[1]);

			if (!persons.isEmpty()) {
				user = persons.get(0);
			}

			if (user != null) {
				Query attendeeQuery = pm.newQuery(Attendee.class);
				attendeeQuery.setFilter("person == nameParam");
				attendeeQuery.declareParameters("String nameParam");

				Attendee attendee = null;

				List<Attendee> attendees = (List<Attendee>) attendeeQuery
						.execute(name[1] + " " + name[0]);

				if (!attendees.isEmpty()) {
					attendee = attendees.get(0);
				} else {
					resp.setStatus(400);
					resp.getWriter().println("No Attendee found");
				}

				Query talkQuery = pm.newQuery(Talk.class);
				talkQuery.setFilter("topic == topicParam");
				talkQuery.declareParameters("String topicParam");

				List<Talk> talks = (List<Talk>) talkQuery.execute(talkParam);
				Talk talk = null;

				if (!talks.isEmpty()) {
					talk = talks.get(0);
				}

				if (talk != null && attendee != null) {
					try {
						pm.refresh(attendee);
						pm.refresh(talk);

						String topic = talk.getTopic();
						String person = attendee.getPerson();

						talk.getAttendees().add(person);
						attendee.getTalks().add(topic);

						pm.makePersistent(attendee);
						pm.makePersistent(talk);
						pm.flush();

						resp.setStatus(200);
						resp.getWriter().println(
								"Registered sucessfully for talk");
					} catch (Exception e) {
						e.printStackTrace();
						resp.setStatus(400);
						resp.getWriter().println("Registration failed");
					} finally {
						pm.close();
					}
				} else {
					resp.setStatus(400);
					resp.getWriter().println("Registration failed");
				}
			}
		}
	}
}
